package com.example.demo.practice.area;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.demo.practice.R;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class AreaLists extends Activity implements View.OnClickListener{
    private Button area, street;
    private ArrayList<Button> btnList = new ArrayList<Button>();
    private FragmentManager fm;
    private FragmentTransaction ar;
    private TextView areas;
    private TextView now;
    private TextView s;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area_list);
        areas = (TextView) findViewById(R.id.areas);
        now = (TextView) findViewById(R.id.now);
        s = (TextView) findViewById(R.id.s);

        findById();

        // 进入系统默认为Area
        fm = getFragmentManager();
        ar = fm.beginTransaction();

        ar.replace(R.id.fragment_content,new Area());
        ar.commit();
    }

    private void findById() {
        area = findViewById(R.id.area_btn);
        street = findViewById(R.id.street_btn);
        area.setOnClickListener(this);
        street.setOnClickListener(this);

        btnList.add(area);
        btnList.add(street);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated methodstub
        fm = getFragmentManager();
        ar = fm.beginTransaction();
        String nows = now.getText().toString();
        String ss = s.getText().toString();
        switch (v.getId()) {

            case R.id.area_btn:
                if(areas.getText().toString().equals("郑州")){
                }else{
                    areas.setText("郑州");
                    now.setText("郑州");
                    s.setText("郑州");
                    //传给fragment控件
                    Area area = new Area();
                    FragmentManager fragmentManager1 = getFragmentManager();
                    FragmentTransaction beginTransaction1 =  fragmentManager1.beginTransaction();
                    beginTransaction1.add(R.id.areas, area);
                    beginTransaction1.addToBackStack(null);
                    ar.replace(R.id.fragment_content,new Area());
                }
                break;

            case R.id.street_btn:
                if (areas.getText().toString().equals("郑州")){
                    areas.setText("登封市");
                    now.setText("登封市");
                    s.setText("登封市");
                    //传给fragment控件
                    Street street = new Street();
                    FragmentManager fragmentManager2 = getFragmentManager();
                    FragmentTransaction beginTransaction2 =   fragmentManager2.beginTransaction();
                    beginTransaction2.add(R.id.areas, street);
                    beginTransaction2.addToBackStack(null);
                    ar.replace(R.id.fragment_content,new Street());
                }else{
                    if (nows.equals(ss)){
                    }else{
                        Street street = new Street();
                        FragmentManager fragmentManager2 = getFragmentManager();
                        FragmentTransaction beginTransaction2 =   fragmentManager2.beginTransaction();
                        beginTransaction2.add(R.id.areas, street);
                        beginTransaction2.addToBackStack(null);
                        ar.replace(R.id.fragment_content,new Street());
                        s.setText(nows);
                    }
                }
                break;

            default:
                break;
        }
        // 提交
        ar.commit();
    }
}
